// pages/details/index.js
const app = getApp();
const fTime=require("../../utils/util.js");
var htmlParse = require('../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      infoData:{
        dg_cover:'',
        dg_title:'',
        dg_desc:'',
        _id:'',
		    loadSuccess:false,
      },
      baseUrl: app.globalData.baseUrl,
      article_content:'',
      userInfo:'',
      commentList:[],
      page: 0,
      limit: 5,
      isLoad: true,
      noData: false,
      textAeraVal:'',
      listCount:0,
      isLogin:false,
      openid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.getInfo(options.id);
    var me = this;
    var openid = wx.getStorageSync('openid') || '';
    this.setData({
      openid: openid,
      userInfo: wx.getStorageSync('userInfo') ||''
    })
    wx.checkSession({
      success:r=>{
        if (openid == '' || !openid) {
            wx.clearStorage();
        }
      },
      fail:r=>{
        wx.clearStorage();
      }
    });
    
  },
  //获取详情
  getInfo:function(id){
    var me=this;
    wx.showLoading({
      title: '加载中',
    });
    wx.request({
      url: app.globalData.baseUrl +'donggu/info',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        did: id,
      },
      success: res => {
        if (res.data.success) {
          this.setData({
            infoData: res.data.result,
			loadSuccess:true,
            article_content: htmlParse.wxParse('article_content', 'html', res.data.result.dg_desc, me, 0)
          });
          this.loadComment();
          wx.hideLoading()
        }
      }
    })
  },
  startLogin(e) {
    var that = this
    console.log(e);
    // 登录
    wx.login({
      success: a => {
        wx.request({
          url: app.globalData.baseUrl + 'donggu/wxlogin',
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            js_code: a.code,
          },
          success: res => {
            wx.setStorageSync('openid', res.data.openid);
            wx.setStorageSync('session_key', res.data.session_key);
            wx.setStorageSync('userInfo',e.detail.userInfo);
            that.setData({
              userInfo: e.detail.userInfo,
              openid: res.data.openid
            })
          }
        })
      }
    })
  },

  bindTextAreaBlur: function (e) {
    console.log(e.detail.value)
    this.data.textAeraVal = e.detail.value
  },
  //添加评论
  addComment:function(){
    var openid = wx.getStorageSync('openid');
    if (openid == '' || !openid){
      wx.showToast({
        title: '未登录',
        icon: 'none',
        duration: 2000
      });
      return false
    }
    if (this.data.textAeraVal==''){
      wx.showToast({
        title: '请填写内容！',
        icon: 'none',
        duration: 2000
      });
      return false
    }
    if (this.data.textAeraVal.length<2) {
      wx.showToast({
        title: '至少两个字哦！',
        icon: 'none',
        duration: 2000
      });
      return false
    }
    wx.showLoading({
      title: '加载中',
    });
    var me=this;
    wx.request({
      url: app.globalData.baseUrl + 'comment/add',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        openid: wx.getStorageSync('openid'),
        dg_id: me.data.infoData._id,
        avatarUrl: me.data.userInfo.avatarUrl,
        nickName: me.data.userInfo.nickName,
        content: me.data.textAeraVal,
        time: fTime.formatTime(new Date()),
        userInfo: me.data.userInfo
      },
      success: res => {
        if (res.data.success) {
          wx.hideLoading()
          
          this.setData({
            textAeraVal:'',
            commentList: [],
            page:0
          });
          this.loadComment();
        }
        wx.showToast({
          title: res.data.msg,
          icon: 'success',
          duration: 2000
        })

      }
    })
  },
  //加载评论
  loadComment: function (){
    var me=this;
    wx.showLoading({
      title: '加载中',
    });
    wx.request({
      url: app.globalData.baseUrl + 'comment/commentList',
      method: 'POST',
      data: {
        page: me.data.page,
        limit: me.data.limit,
        dg_id: me.data.infoData._id,
        listType: 'fenlei'
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        let resData = res.data;
        if (resData.success) {
          wx.hideLoading();
          let resList = res.data.result.list || [];
          this.setData({
            commentList: [...this.data.commentList, ...resList],
            listCount: res.data.result.count
          });
          if (resList.length < this.data.limit) {
            
            this.setData({
              noData: true,
              isLoad: false
            });
            return false;
          }
          this.data.page++;

        }
        else{
          this.setData({
            noData: true,
            isLoad: false
          });
        }
      }
    })
  },

  checkDel: function (cid){
    console.log(cid.currentTarget.dataset.cid)
    wx.showModal({
      title: '提示',
      content: '确定删除这条评论吗？',
      success:  res=> {
        if (res.confirm) {
          this.delComment(cid.currentTarget.dataset.cid);
        } else if (res.cancel) {
          //console.log('用户点击取消')
        }
      }
    })
  },

  //删除自己添加的评论
  delComment: function (cid){
    var me = this;
    wx.showLoading({
      title: '加载中',
    });
    wx.request({
      url: app.globalData.baseUrl + 'comment/delComment',
      method: 'POST',
      data: {
        comment_id:cid.toString()
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        
        let resData = res.data;
        if (resData.success) {
          
          wx.showToast({
            title: resData.msg,
            icon: 'none',
            duration: 2000
          });
          this.setData({
            commentList: [],
            page: 0
          });
          this.loadComment();
        }
        else{
          wx.showToast({
            title: resData.msg,
            icon: 'none',
            duration: 2000
          });
        }
        wx.hideLoading();
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.infoData.dg_title,
      desc: this.data.infoData.dg_desc.substr(0, 30),
      path: '/pages/details/details?id=' + this.data.infoData._id,
      imageUrl: app.globalData.baseUrl + this.data.infoData.dg_cover,
      fail: function (res) {
        
      },
    }
  },
  
})